<template>
  <div class="content">
    <div style="display: flex">
      <jc-button
        :btn-arr="['reload', 'save', 'option']"
        :disab-arr="['close', 'cancel', 'freeze', 'com1', 'com2', 'using']"
        @refresh="handleGetBomList"
        @save="save"
        @toExcel="handleExport"
      />
      <el-button
        style="margin-left: 10px;"
        type="primary"
        size="mini"
        @click="billVis=true"
      >{{ $t('market.batchEdit') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQueryBomList"
        />
        <el-button
          type="primary"
          class="btn"
          size="mini"
          @click="handleQueryBomList"
        >
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          :select-data="selectData"
          @seachData="handleQueryBomList"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        serial
        sortable
        :tooltip="false"
        table-selection
        get-row-keys="FID"
        @selectionChange="selectionDatas"
      >
        <template v-slot:billSlot="col">
          <el-image
            style="width: 30px;height: 30px;"
            :src="$img + col.scope.row.fimg"
            :preview-src-list="[$img + col.scope.row.fimg]"
          />
        </template>
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row.FNUMBER)">
            {{ col.scope.row.FNUMBER }}
          </el-link>
        </template>
        <!--人工计划成本-->
        <template v-slot:text2Slot="col">
          <el-input-number v-model="col.scope.row.ARTIFICIALSUM" size="mini" />
        </template>
        <!--生产工时-->
        <template v-slot:text3Slot="col">
          <el-input-number v-model="col.scope.row.WORKINGHOURS" size="mini" />
        </template>
        <!--审核状态-->
        <template v-slot:btnStates="col">
          <el-tag :type="col.scope.row.stateColor">{{ col.scope.row.FDOCUMENTSTATUS }}</el-tag>
        </template>
        <!--禁用状态-->
        <template v-slot:tagSlot="clo">
          <el-tag v-if="clo.scope.row.FFORBIDSTATUS === '否'">
            {{ clo.scope.row.FFORBIDSTATUS }}
          </el-tag>
          <el-tag v-else type="danger">
            {{ clo.scope.row.FFORBIDSTATUS }}
          </el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="handleGetBomList"
      />
    </div>
    <!--  批量修改  -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.batchEdit')"
      :visible.sync="billVis"
      width="400px"
      top="15vh"
      center
      :close-on-click-modal="false"
      close-on-press-escape
    >
      <div class="el-dialog__body">
        <span>{{ $t('market.artificialsum') }}<!--生产人工--> </span>
        <el-input-number v-model="laborcost" size="mini" :min="0" controls-position="right" />
        <div style="height: 3px;" />
        <span>{{ $t('market.workinghours') }}<!--生产工时--> </span>
        <el-input-number v-model="laborcost1" size="mini" :min="0" controls-position="right" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="billVis = false">{{ $t('btn.cancel') }}</el-button>
        <el-button size="mini" type="primary" @click="save(1)">{{ $t('btn.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTEngBomList,
  updateBomPro
} from '@/api/engineering/bomList'
import { Forbid, toDocument } from '@/components/ToMxamineState'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
export default {
  name: 'ListBom',
  components: {
    search,
    jcButton,
    filtration
  },
  mixins: [searData],
  props: {
    fnumber: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ftype: 13,
      fbillNo: 'fnumber', // 编码
      total: 0, // 总条目
      pageNum: 1, // 当前页
      pageSize: 200, // 每页显示多少条数据
      billVis: false,
      laborcost: 1,
      laborcost1: 1,
      // 表头
      tableHeader: [
        { label: this.$t('shop.image'), type: 'bill', prop: 'fimg', width: '100' },
        { label: this.$t('market.fmodel'), prop: 'FMODEL' },
        { label: this.$t('market.fmaterial'), type: 'order', prop: 'fnumber', width: '120' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'FSPECIFICATION' },
        { label: this.$t('market.fdescripTion'), prop: 'FDESCRIPTION', minWidth: '200px' },
        { label: this.$t('market.createDate'), prop: 'fcreatedate' },
        { label: this.$t('market.artificialsum'), prop: 'ARTIFICIALSUM', type: 'text2' },
        { label: this.$t('market.workinghours'), prop: 'WORKINGHOURS', type: 'text3' },
        { label: this.$t('market.forbidStatu'), type: 'tag', prop: 'FFORBIDSTATUS' },
        { label: this.$t('market.documentstatus'), prop: 'FDOCUMENTSTATUS', type: 'states' }
        // { label: this.$t('market.handle'), type: 'btn', width: '120px' }
      ],
      // 表格数据
      tableData: [],
      tableHeader1: [
        { label: this.$t('market.funit'), prop: 'funit' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.modifier'), prop: 'fmodifier' },
        { label: this.$t('market.modifyDate'), prop: 'fmodifydate' },
        { label: this.$t('market.approver'), prop: 'fapprover' },
        { label: this.$t('market.approveDate'), prop: 'fapprovedate' }
      ],
      age: 1,
      newAge: 5
    }
  },
  watch: {
    fnumber() {
      this.handleGetBomList()
    }
  },
  created() {
    !this.fnumber && this.handleGetBomList()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          getTEngBomList({
            pageNum: 1,
            pageSize: 100000,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array.map(item => {
                return (toDocument(item), Forbid(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '定额因子')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 进入物料主数据
    jumpMateriel(FNUMBER) {
      this.$router.push({ path: `/detailMateriel/${FNUMBER}` })
    },
    // 获取列表数据
    async handleGetBomList(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        search: this.searCollData.concat(this.details)
      }
      if (this.fnumber) {
        DATA.fnumber = this.fnumber
        DATA.fforbidStatus = 'A' // 禁用: 否
        DATA.fdocumentStatus = 'C' // 已审核
      }
      const { data: RES } = await getTEngBomList(DATA)
      this.tableData = RES.array.map(item => {
        return (toDocument(item), Forbid(item))
      })
      this.total = RES.total
      this.loading = false
    },
    // 搜索
    handleQueryBomList(ev) {
      this.pageNum = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetBomList(ev.detail)
      } else {
        this.handleGetBomList()
      }
    },
    // 多选时触发的事件
    selectionDatas(val) {
      this.selectVal = []
      this.val = val
      if (val.length > 0) {
        val.forEach(res => {
          res.FNUMBER && this.selectVal.push({
            ARTIFICIALSUM: res.ARTIFICIALSUM,
            WORKINGHOURS: res.WORKINGHOURS,
            FID: res.FID
          })
        })
      }
    },
    // 修改人工成本
    save(ev) {
      if (ev === 1) {
        this.selectVal.map(res => {
          res.ARTIFICIALSUM = this.laborcost
          res.WORKINGHOURS = this.laborcost1
        })
      }
      const DATA = {
        fTreeEntity: this.selectVal
      }
      updateBomPro(DATA).then(res => {
        if (res.code === 0) {
          this.billVis = false
          this.handleGetBomList()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
  .productImg {
    width: auto;
    height: 40px;
  }
  .el-dialog .el-dialog__body {
    text-align: center;
  }
  .image {
    width: auto;
    height: 100%;
    max-height: 100%;
    max-width: 100%;
  }
}
</style>
